package com.mzkj.wanjulaile.mobile.api.modules.stock.service;


import com.mzkj.wanjulaile.mobile.api.modules.stock.entity.Stock;
import com.mzkj.wanjulaile.mobile.api.modules.stock.mapper.StockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 库存Service
 * @author ianly
 * @version 2016-10-09
 */
@Service
@Transactional(readOnly = true)
public class StockServiceImpl implements  StockService{
	@Autowired
	private StockMapper stockMapper;

	public Stock get(String id) {
		return stockMapper.get(id);
	}
	
	@Transactional(readOnly = false)
	public void saveCurrent(Stock stock){
		Stock s=stockMapper.getByToyId(stock.getToy().getId());
		if(s!=null){
			int realCount=s.getRealCount()==null?0:s.getRealCount();
			stock.setRealCount(realCount+stock.getRealCount());
			stock.setId(s.getId());
			stock.preUpdate();
			stockMapper.update(stock);
			//到货提醒
			//TODO
//			sendMsgService.toyNotice(stock.getToy().getId());
		}
	}

	/**
	 * 根据玩具id获取该玩具库存数量
	 * @param toyId
	 * @return
     */
	public Stock getByToyId(String toyId) {
		return stockMapper.getByToyId(toyId);
	}
}